DrawIcon

part of wslib

a Pen-based library system for drawing iconographic pictures.

Note : DrawIcon is partially compatible with GUI-style coding. Colors and text are not supported

DrawIcon contains a global library with pen functions. It draws the icon in a Rect you specify.

DrawIcon returns a Pen function.

Used by RoundButton an SCAlert

DrawIcon( iconName, rect ... extra args )

(

w = GUI.window.new( "DrawIcon" ).front;

w.drawHook = { |window| DrawIcon( \speaker, window.view.bounds.insetBy(10,10) ) };

w.refresh;

)

(

w.drawHook = { |window|

Color.red(0.5).alpha_(0.5).set;

DrawIcon( \speaker, window.view.bounds.insetBy(10,10) ) };

w.refresh;

)

The library of Pen functions is stored in drawFuncs

DrawIcon.names.dopostln; // post all available iconNames

(

w = GUI.window.new( "drawFuncs" ).front;

k = DrawIcon.drawFuncs.keys.asArray

.select({ |item| ['-', '!'].includes(item).not }) //SCPopUpMenu doesn't like '-' and '!'

.sort;

n = '+';

w.drawHook = { |window|

DrawIcon( n, window.view.bounds.insetBy(10,10) ) };

m = GUI.popUpMenu.new( w, Rect(5,5,80,20) )

.items_( k )

.action_({ |popUp|

n = k.at( popUp.value );

w.refresh; });

w.refresh;

)

Some of the drawFuncs have optional extra arguments. These can be added after the Rect in DrawIcon

(

// arrow has arguments 'direction' and 'mode'

w = SCWindow( "DrawIcon" ).front.decorate;

a = 0;

m = 'fill';

w.drawHook = { |window| DrawIcon( \arrow, window.view.bounds.insetBy(10,10), a, m ) };

b = SmoothSlider( w, 80@20)

.action_({ |sl| a = sl.value.linlin(0,1,0,2pi); w.refresh; });

c = RoundButton( w, 80@20 )

.states_([[ "fill" ], ["stroke"]])

.action_({ |bt| m = [\fill, \stroke][ bt.value]; w.refresh });

w.refresh;

)

(

// clock has arguments 'h', 'm' and 'secs'

w = SCWindow( "DrawIcon" ).front;

d = Date.localtime;

w.drawHook = { |window| DrawIcon( \clock, window.view.bounds.insetBy(10,10),

d.hour, d.minute, d.second ) };

t = Task({ loop { d.localtime;

{ if( w.notNil && { w.dataptr.notNil }) { w.refresh; } }.defer;

1.0.wait }

}).start;

w.onClose_({ t.stop; });

w.refresh;

)

(

// post all keyFuncs with extra argNames, with their default values

DrawIcon.drawFuncs.keys

.collect({ |key| [ key, DrawIcon.drawFuncs[ key ]] })

.select({ |item| item[1].def.argNames.size > 1 })

.do({ |item|

var argNames, argValues;

argNames = item[1].def.argNames[1..];

argValues = item[1].def.prototypeFrame[1..];

argNames = argNames.collect({ |name,i|

if( argValues[ i ].notNil )

{ name ++ " = " ++ argValues[ i ].asCompileString }

{ name }

});

"'%' ( % )\n".postf( item[0], argNames.join( ", ") )

}); "";

)

*symbolArgs

The symbolArgs creation method is a special creation mode where extra arguments

are encoded in the name of the drawFunc. This is used by RoundButton and SCAlert

The encoding goes as follows:

'iconName_firstArg_secondArg_etc'

The args are interpreted before they are used, so they can be expressions

(

w = GUI.window.new( "DrawIcon" ).front;

w.drawHook = { |window| DrawIcon.symbolArgs( 'arrow_0.5pi', window.view.bounds.insetBy(10,10) ) };

w.refresh;

)

(

w.drawHook = { |window| DrawIcon.symbolArgs( 'led_250', window.view.bounds.insetBy(10,10) ) };

w.refresh;

)

When args are symbols themselves you need to add \' signs

(

w.drawHook = { |window| DrawIcon.symbolArgs( 'play_\'stroke\'',

window.view.bounds.insetBy(10,10) ) };

w.refresh;

)